package com.csxy.nyf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.nyf.entity.EduCourse;
import com.csxy.nyf.entity.EduCourseDescription;
import com.csxy.nyf.entity.vo.*;
import com.csxy.nyf.exception.MyException;
import com.csxy.nyf.mapper.EduCourseMapper;
import com.csxy.nyf.service.EduChapterService;
import com.csxy.nyf.service.EduCourseDescriptionService;
import com.csxy.nyf.service.EduCourseService;
import com.csxy.nyf.service.EduVideoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    // 向课程表里面添加课程基本信息
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    @Autowired
    private EduCourseMapper eduCourseMapper;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private EduVideoService eduVideoService;
    @Override
    public String addCourseInfo(CourseInfoForm courseInfoForm) {
        // 向课程表里面添加课程基本信息
        //CourseInfoForm对象，转换成EduCourse对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm,eduCourse);
        int result = baseMapper.insert(eduCourse);

        if (result <= 0) {
        // 表示添加失败
         throw new MyException(20001,"添加课程信息失败");
         }

        //获取添加之后课程信息的id
        String cid = eduCourse.getId();
        //向课程简介表里面添加课程简介信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        //手动设置描述课程的id,与上面的课程信息id一致
        eduCourseDescription.setId(cid);
        //eduCourseDescriptionMapper.insert(eduCourseDescription);
        return cid;
    }

    @Override
    public CourseInfoForm getCourseInfo(String courseId) {
        //定义一个空的课程对象
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        //根据id查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        //属性拷贝
        BeanUtils.copyProperties(eduCourse,courseInfoForm);
        //查询简介并复制
        courseInfoForm.setDescription(eduCourseDescriptionService.getById(courseId).getDescription());
        return courseInfoForm;
    }

    @Override
    public void updateCourseInfo(CourseInfoForm courseInfoForm) {
        //1修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm,eduCourse);
        baseMapper.updateById(eduCourse);
        //2修改描述信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        eduCourseDescription.setId(courseInfoForm.getId());
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }

    @Override
    public boolean removeCourse(String id) {

        //1、根据课程id删除小节
        eduVideoService.removeVideoByCourseId(id);

        //2、根据课程id删除章节
        eduChapterService.removeChapterByCourseId(id);

        //3、根据课程id删除课程描述
        eduCourseDescriptionService.removeById(id);
        //4、根据课程id删除课程本身
        int result = baseMapper.deleteById(id);
        if(result > 0){
            return true;
        }else {
            throw new MyException(20001,"删除失败");
        }
    }

    @Override
    public void pageQuery(Page<EduCourse> pageParm, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        //取出值，判断他们是否有值
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        //判断条件是否为空，如果不为空，拼接条件
        //判断是否有传入教师名
        if(!title.isEmpty()) {
            //构造条件
            queryWrapper.like("title",title); //参数1：数据库字段名，参数2：模糊查询的值
        }
        //判断是否有传入状态
        if(!status.isEmpty()) {
            //构造条件
            queryWrapper.eq("status",status);
        }
        //降序排序
        queryWrapper.orderByDesc("gmt_modified");
        //带上门判断后的条件进行分页查询
        baseMapper.selectPage(pageParm,queryWrapper);
    }

    @Override
    public CoursePublishVo getPublishCourseInfo(String id) {
        return eduCourseMapper.getPublishCourseInfo(id);
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }

    @Override
    @Cacheable(value = "course",key = "'selectHotCourse'")
    public List<EduCourse> selectHotCourse() {
        //查询前8个最热门的课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("view_count");
        wrapper.last("limit 8");
        return baseMapper.selectList(wrapper);
    }

    @Override
    public Map<String, Object> getCourseFrontInfo(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {

        //拼接条件查询
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(courseFrontVo.getSubjectParentId())){
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if (StringUtils.isNotEmpty(courseFrontVo.getSubjectId())){
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if (StringUtils.isNotEmpty(courseFrontVo.getBuyCountSort())){
            wrapper.orderByDesc("buy_count");
        }
        if (StringUtils.isNotEmpty(courseFrontVo.getPriceSort())){
            wrapper.orderByDesc("price");
        }
        if (StringUtils.isNotEmpty(courseFrontVo.getGmtCreateSort())){
            wrapper.orderByDesc("gmt_create");
        }
        eduCourseMapper.selectPage(pageCourse,wrapper);

        Map<String,Object> map = new HashMap<>();
        long total = pageCourse.getTotal();//总记录数
        List<EduCourse> courseList = pageCourse.getRecords();//数据集合
        long size = pageCourse.getSize();//每页记录数
        long current = pageCourse.getCurrent();//当前页
        long pages = pageCourse.getPages();//总页数
        boolean hasPrevious = pageCourse.hasPrevious();//是否有上一页
        boolean hasNext = pageCourse.hasNext();//是否有下一页
        map.put("total",total);
        map.put("list",courseList);
        map.put("size",size);
        map.put("current",current);
        map.put("pages",pages);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);
        return map;
    }
}